<?php


/**************商品货位***************/

class GoodsplaceAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(4,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function index(){
		//货位列表
		import('ORG.Util.Category');
		$cat = new Category('Goods_place', array('place_id', 'parent', 'place_name', 'fullname'));
		$this->place_list=$cat->getList();
		$this->place_list2=$cat->getList('',0,'place_name');
        //添加时的列表
		$Place=M('Goods_place');
		$this->place=$Place->where('parent=0')->order('place_id desc')->select();
		$this->display();
	}
    //添加货位
	public function add(){
		$Place=M('Goods_place');
		$pid=$this->_post('pid');
		$name=$this->_post('name');
		if(empty($name)) $this->error("名称不得为空！");

		$father=$Place->where('place_id='.$pid)->find();
		$map['place_name']=$name;
		$map['parent']=$pid;
		$check=$Place->where($map)->select();
		if($check){
			$this->error("此货位已经存在！");
		}else{
		    $data2['place_name']=$name;
		    $data2['parent']=$pid;
			if($father){
			$data2['path']=$father['path'];
			}
			$result=$Place->add($data2);

			$now_place=$Place->where('place_id='.$result)->find();
			$result2=$data['path']=$now_place['path'].",".$result;
 
			$Place->where('place_id='.$result)->save($data);
			if($result2) {$this->success("添加成功！");}else{$this->error("添加失败！");}
		}
	}

	//更改排序
	public function sorting(){
		$place_id=$this->_post('place_id');
		$sorts=$this->_post('sort');
		$Place=M('Goods_place');
		for($i=0;$i<count($place_id);$i++){
			$data['sorting']=$sorts[$i];
			$Place->where('place_id='.$place_id[$i])->save($data);
		}
	}

	//货位修改
	public function edit(){
		$Place=M('Goods_place');
		$place_id=$this->_get('place');
		$this->now_place=$Place->where('place_id='.$place_id)->find();
		$this->place_list=$this->get_list();

		$this->display();
	}
	public function save_edit(){
		$Place=M('Goods_place');
		$place_id=$this->_post('place_id');
		$parent=$this->_post('parent');

		$father=$Place->where('place_id='.$parent)->find();
		$data['path']=$father['path'].",".$place_id;
		$data['parent']=$parent;
		$data['place_name']=$this->_post('place_name');
		$result=$Place->where('place_id='.$place_id)->save($data);
		if($result){
			echo '1';
		}else{
			echo '0';
		}
	}

	public function get_list() {
		import('ORG.Util.Category');
        $cat = new Category('Goods_place', array('place_id', 'parent', 'place_name', 'fullname'));
		return $cat->getList('',0,'sorting');
	}

	public function deletePlace()
	{
		$map['place_id'] = $this->_get('place_id');
		$p = M('Goods_place');
		$res = $p->where($map)->delete();
		if($res){
			echo 0;
		}else{
			echo -1;
		}
	}


}
?>